package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.query.MenuQueryObject;
import cn.wolfcode.crm.service.IMenuService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"menu:view","菜单页面"},logical = Logical.OR)
    public String view() throws Exception{

        return "menu/menu";
    }

    @RequestMapping("selectParent")
    @ResponseBody
    public Menu selectParent(Long id) throws Exception{
        return menuService.selectByPrimaryKey(id);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Menu> selectAll() throws Exception{
        return menuService.selectAll();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"menu:saveOrUpdate","菜单新增/修改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Menu entity) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            menuService.saveOrUpdate(entity );
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"menu:delete","菜单删除"},logical = Logical.OR)
    public JsonResult delete(Long id) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            menuService.deleteByPrimaryKey(id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("selectByParentSn")
    @ResponseBody
    public List<Menu> selectByParentSn(String sn) {

        return menuService.selectByParentSn(sn);

    }


    @RequestMapping("list")
    @ResponseBody
    public PageResult list(MenuQueryObject qo) throws Exception{
        return menuService.query(qo);
    }

}
